#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int st[2 * N];
LL s[N];
int w[N];
int n, m;
int main()
{
	cin >> n;
	//int cnt_1 = 0, cnt_0 = 0;
	//for (int i = 1; i <= n; i++) {
	//	cin >> w[i];
	//	if (w[i] == 1)cnt_1++;
	//	else cnt_0++;
	//}
	//int ans = 0;
	///*for (int i = 1; i <= n - 1; i++) {
	//	if (st[i])continue;
	//	if (w[i] != w[i + 1]) {
	//		st[i] = st[i + 1] = 1;
	//		ans += 2;
	//	}
	//}*/
	//cout << min(cnt_0, cnt_1) * 2 << endl;
	bool flag = 0;
	for (int i = 1; i <= n; i++) {
		cin >> w[i];
		if (!w[i])s[i] = s[i - 1] - 1;
		else s[i] = s[i - 1] + 1;
		if (i >= 2) {
			if (w[i] != w[i - 1])flag = 1;
		}
	}
	if (!flag) {
		cout << 0 << endl;
		return 0;
	}
	int ans = 0;
	/*for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= i; j++) {
			if (s[i] - s[j - 1] == (i - j + 1) / 2) {
				ans = max(ans, (s[i] - s[j - 1]) * 2);
			}
		}
	}*/
	memset(st, 0x3f, sizeof(st));
	ans = 0;
	st[s[1] + N] = 1;
	st[N] = 0;
	for (int i = 2; i <= n; i++) {
		if (st[s[i] + N] != 0x3f3f3f3f) {
			ans = max(ans, i - st[s[i] + N]);
		}
		//cout << "st[s[i]]===" << st[s[i] + N] << endl;
		//cout << "s[i]===" << s[i] << endl;
		st[s[i] + N] = min(st[s[i] + N], i);
	}
	cout << ans << endl;
	return 0;
}